/**
 * @copyright chuzhixin 1204505056@qq.com
 * @description router全局配置，如有必要可分文件抽离
 */

import Vue from "vue";
import VueRouter from "vue-router";
import Layout from "@/layouts";
import EmptyLayout from "@/layouts/EmptyLayout";
import { routerMode } from "@/config/settings";

Vue.use(VueRouter);

export const constantRoutes = [
  {
    path: "/login",
    component: () => import("@/views/login/index"),
    hidden: true,
  },
  {
    path: "/register",
    component: () => import("@/views/register/index"),
    hidden: true,
  },
  {
    path: "/401",
    name: "401",
    component: () => import("@/views/401"),
    hidden: true,
  },
  {
    path: "/404",
    name: "404",
    component: () => import("@/views/404"),
    hidden: true,
  },
];

/*当settings.js里authentication配置的是intelligence时，views引入交给前端配置*/
export const asyncRoutes = [
  {
    path: "/",
    component: Layout,
    redirect: "/index",
    children: [
      {
        path: "/index",
        name: "Index",
        component: () => import("@/views/index/index"),
        meta: {
          title: "首页",
          icon: "home",
          affix: true,
          noKeepAlive: true,
        },
      },
    ],
  },
  {
    path: "/editingArticles",
    redirect: "noRedirect",
    component: Layout,
    children: [
      {
        path: "/editingArticles",
        name: "EditingArticles",
        component: () => import("@/views/project/editingArticles/index"),
        meta: {
          title: "发布文章",
          icon: "edit",
        },
      },
    ],
  },
  {
    path: "/articlesList",
    component: Layout,
    redirect: "noRedirect",
    children: [
      {
        path: "/articlesList",
        name: "ArticlesList",
        component: () => import("@/views/project/articlesList/index"),
        meta: {
          title: "文章管理",
          icon: "book-open",
        },
      },
    ],
  },
  {
    path: "/comment",
    redirect: "noRedirect",
    component: Layout,
    children: [
      {
        path: "/comment",
        name: "Comment",
        component: () => import("@/views/project/comment/index"),
        meta: {
          title: "用户评论",
          icon: "comments",
        },
      },
    ],
  },
  {
    path: "/admin",
    component: Layout,
    redirect: "noRedirect",
    name: "Admin",
    meta: { title: "管理员操作", icon: "box-open" },
    children: [
      {
        path: "/user",
        name: "User",
        component: () => import("@/views/project/user/index"),
        meta: {
          title: "用户管理",
          icon: "user",
        },
      },

      {
        path: "creater",
        name: "Creater",
        component: () => import("@/views/project/creater/index"),
        meta: {
          title: "博主申请",
          icon: "user",
        },
      },
    ],
  },

  {
    path: "*",
    redirect: "/404",
    hidden: true,
  },
];

const router = new VueRouter({
  mode: routerMode,
  scrollBehavior: () => ({
    y: 0,
  }),
  routes: constantRoutes,
});
/* const originalPush = VueRouter.prototype.push;
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch((err) => err);
}; */

export function resetRouter() {
  router.matcher = new VueRouter({
    mode: routerMode,
    scrollBehavior: () => ({
      y: 0,
    }),
    routes: constantRoutes,
  }).matcher;
}

export default router;
